{
 "nbformat": 4,
 "nbformat_minor": 2,
 "metadata": {
  "language_info": {
   "name": "python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "version": "3.7.4-final"
  },
  "orig_nbformat": 2,
  "file_extension": ".py",
  "mimetype": "text/x-python",
  "name": "python",
  "npconvert_exporter": "python",
  "pygments_lexer": "ipython3",
  "version": 3,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  }
 },
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": "Pregnancies  Glucose  BloodPressure  SkinThickness  Insulin   BMI  \\\n0            6      148             72             35        0  33.6   \n1            1       85             66             29        0  26.6   \n2            8      183             64              0        0  23.3   \n3            1       89             66             23       94  28.1   \n4            0      137             40             35      168  43.1   \n\n   DiabetesPedigreeFunction  Age  Outcome  \n0                     0.627   50        1  \n1                     0.351   31        0  \n2                     0.672   32        1  \n3                     0.167   21        0  \n4                     2.288   33        1  \n"
    }
   ],
   "source": [
    "# 特征工程\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "train = pd.read_csv('pima-indians-diabetes.csv')\n",
    "print(train.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": "Glucose          0\nBloodPressure    0\nSkinThickness    0\nInsulin          0\nBMI              0\ndtype: int64\nPregnancies                   0\nGlucose                       5\nBloodPressure                35\nSkinThickness               227\nInsulin                     374\nBMI                          11\nDiabetesPedigreeFunction      0\nAge                           0\nOutcome                       0\ndtype: int64\n"
    }
   ],
   "source": [
    "# 存在数据缺失得字段\n",
    "NaN_col_names = ['Glucose','BloodPressure','SkinThickness','Insulin','BMI']\n",
    "train[NaN_col_names] = train[NaN_col_names].replace(0, np.NaN)\n",
    "print((train[NaN_col_names] == 0).sum())\n",
    "print(train.isnull().sum())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "用中值填补缺失值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": "Pregnancies                 0\nGlucose                     0\nBloodPressure               0\nSkinThickness               0\nInsulin                     0\nBMI                         0\nDiabetesPedigreeFunction    0\nAge                         0\nOutcome                     0\ndtype: int64\n"
    }
   ],
   "source": [
    "medians = train.median()\n",
    "train = train.fillna(medians)\n",
    "\n",
    "print(train.isnull().sum())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "标准化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": "[[ 0.63994726  0.86604475 -0.03198993 ...  0.16661938  0.46849198\n   1.4259954 ]\n [-0.84488505 -1.20506583 -0.5283186  ... -0.85219976 -0.36506078\n  -0.19067191]\n [ 1.23388019  2.01666174 -0.69376149 ... -1.33250021  0.60439732\n  -0.10558415]\n ...\n [ 0.3429808  -0.02157407 -0.03198993 ... -0.910418   -0.68519336\n  -0.27575966]\n [-0.84488505  0.14279979 -1.02464727 ... -0.34279019 -0.37110101\n   1.17073215]\n [-0.84488505 -0.94206766 -0.19743282 ... -0.29912651 -0.47378505\n  -0.87137393]]\n"
    }
   ],
   "source": [
    "from sklearn.preprocessing import StandardScaler\n",
    "std = StandardScaler()\n",
    "\n",
    "y_train = train['Outcome']\n",
    "x_train = train.drop(['Outcome'], axis=1)\n",
    "feat_columns = x_train.columns\n",
    "\n",
    "x_train = std.fit_transform(x_train)\n",
    "print(x_train)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "保存处理后的文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_train = pd.DataFrame(data=x_train,columns=feat_columns)\n",
    "train = pd.concat([x_train,y_train], axis=1)\n",
    "\n",
    "train.to_csv('fe_data.csv', index=False, header=True)"
   ]
  }
 ]
}